<?php

function getSelectedTeamForCurrentRound($teamId) {
	$currentRound = getCurrentRound();
    $roundForSelectedTeamsJoin = getCurrentRound();
	$team = getTeamById($teamId, false);
	
	$sql = "select count(*) as numberPlayers from playerforround where round = $currentRound and playerId in (select id from teamplayer where teamId = $teamId and traded = false);";
	$result = mysql_query($sql);
	$row = mysql_fetch_array($result);
	$numberPlayers = $row['numberPlayers'];
	
	if ($numberPlayers == 0) {
		if ($currentRound <= 2) {
			return getSelectedTeam($teamId);
		}
		
		if ($currentRound == 13 || $currentRound == 14) {
			$currentRound = 10;
		}
		else {
			$currentRound -= 1;
		}
	}
	
	$sql = "select p.id as playerId, t.coachName, p.firstName, p.surname, pfr.playerPosition, p.playerType, p.team, tp.isLti, tp.isLtiReplacement, ast.position from teamplayer tp";
	$sql .= " left join playerforround pfr on pfr.playerId = tp.id and round = $currentRound inner join team t on tp.teamId = t.id inner join player p on tp.playerId = p.id";
	$sql .= " left join aflselectedteams ast on ast.playerId = p.id and ast.round = $roundForSelectedTeamsJoin where tp.teamId = $teamId and traded = false";
	
	$result = mysql_query($sql);
	
	$array = array();
	while($row = mysql_fetch_array($result)) {
		$array[] = getSelectedPlayer($row);
	}
	
	
	return $array;
}

function getSelectedTeamForRound($teamId, $round) {
	$team = getTeamById($teamId, false);
	
	$sql = "select count(*) as numberPlayers from playerforround where round = $round and playerId in (select id from teamplayer where teamId = $teamId and traded = false);";
	$result = mysql_query($sql);
	$row = mysql_fetch_array($result);
	$numberPlayers = $row['numberPlayers'];
	
	if ($numberPlayers == 0) {
		if ($round <= 2) {
			return getSelectedTeam($teamId);
		}
		
		$round -= 1;
	}
	
	$sql = "select p.id as playerId, t.coachName, p.firstName, p.surname, pfr.playerPosition, p.playerType, p.team, tp.isLti, tp.isLtiReplacement, ast.position from teamplayer tp";
	$sql .= " left join playerforround pfr on pfr.playerId = tp.id and round = $round inner join team t on tp.teamId = t.id inner join player p on tp.playerId = p.id";
	$sql .= " left join aflselectedteams ast on ast.playerId = p.id and ast.round = $round where tp.teamId = $teamId";
	
	$result = mysql_query($sql);
	
	$array = array();
	while($row = mysql_fetch_array($result)) {
		$array[] = getSelectedPlayer($row);
	}
	
	
	return $array;
}

function clearPositionsForRound($teamId, $round) {
	$sql = "DELETE from playerforround where round = $round and playerId in (select id from teamplayer where teamId = $teamId);";
	
	mysql_query($sql);
	
}

function setPositionForRound($teamId, $playerId, $position, $round) {
	if ($playerId != -1) {
			if (substr($position, 0, 1) == "E") {
			$sql = "INSERT into playerforround (playerId, round, emergency, playerPosition) VALUES ((select id from teamplayer";
			$sql .= " where playerId = $playerId and teamId = $teamId and traded = false), $round, true, '$position');";		
		}
		else {
			$sql = "INSERT into playerforround (playerId, round, playerPosition) VALUES ((select id from teamplayer where playerId = $playerId and teamId = $teamId and traded = false), $round, '$position');";
		}
		
		mysql_query($sql);
		
		}
}

function getPlayersForRound($teamId, $roundId, $isArchive) {
 	$playerForRoundSql = "SELECT tp.playerId as playerId, tp2.playerId as replacementPlayerId, tp.teamId, roundId from playerforround" . getTableExtension($isArchive) . " pfr";
 	$sql .= " inner join teamplayer" . getTableExtension($isArchive) . " tp on pfr.playerId = tp.id left join teamplayer" . getTableExtension($isArchive) . " tp2 on pfr.replacementPlayerId = tp2.id";
 	$sql .= " where tp.teamId = $teamId and roundId = $roundId and emergency = false;";
   	$result = mysql_query($playerForRoundSql);
	
	$playerForRoundArray = array();
	while($row = mysql_fetch_array($result)) {
		$playerForRound = new PlayerForRound();
		$playerForRound->teamId = $row["teamId"];
		$playerForRound->playerId = $row["playerId"];
		$playerForRound->replacementPlayerId = $row["replacementPlayerId"];
		$playerForRound->round = $row["roundId"];
		
		$playerForRoundArray[] = $playerForRound; 	
	}       	
	
      	
   	return $playerForRoundArray;
}

function getInvalidTeams($leagueId) {
        $partialLockoutTeams = getPartialLockoutTeams();
    $currentRoundStatus = getCurrentRoundStatus();
    $reqString = "";
    $firstOne = true;
    foreach ($partialLockoutTeams as $partialLockoutTeam) {
       if (!$firstOne) {
          $reqString .= ",";
       }
       else {
          $firstOne = false;
       }
       $reqString .= "'" . $partialLockoutTeam . "'";
    }
	$round = getCurrentRound();
	$sql = "SELECT coachName, firstName, surname, team, playerType FROM playerforround pfr inner join teamplayer tp on pfr.playerId = tp.id ";
	$sql .= "inner join player p on tp.playerId = p.id ";
	$sql .= "inner join team t on tp.teamId = t.id and leagueId = $leagueId ";
	$sql .= "WHERE pfr.round = $round and p.id not in (select playerId from aflselectedteams where round = $round and position <> 'EMG') ";
	$sql .= " and p.team in (select distinct p.team from aflselectedteams ast inner join player p on ast.playerId = p.id where round = $round)";
	$sql .= " and pfr.playerPosition not in ('E1', 'E2', 'E3', 'E4', 'E5')";
	if ($currentRoundStatus == RoundStatusType::Partial_Lockout) {
    	$sql .= " and p.team not in ($reqString)";
	}
	$result = mysql_query($sql);
	
	$lastTeam = "";
	$invalidTeam = null;
	$invalidTeams = array();
	while($row = mysql_fetch_array($result)) {
		$coachName = $row['coachName'];
		if ($lastTeam != $coachName) {
			if ($invalidTeam != null) {
				$invalidTeams[] = $invalidTeam;
			}
			$invalidTeam = new InvalidTeam();
			$invalidTeam->coachName = $coachName;
			$invalidTeam->invalidPlayers = array();
                        $lastTeam = $coachName;
		}
		$player = new Player();
		$player->playerFirstName = $row['firstName'];
		$player->playerSurname = $row['surname'];
		$player->team = $row['team'];
		$player->playerType = $row['playerType'];
		$invalidTeam->invalidPlayers[] = $player;
	}
        if ($invalidTeam != null) {
	$invalidTeams[] = $invalidTeam;
        }
	
	return $invalidTeams;
}

function replaceZeroScoringPlayer($playerId, $replacementPlayerId, $round, $replacementTypeDescription) {
	$sql = "UPDATE playerforround set replacementPlayerId = $replacementPlayerId, replacementDescription = '$replacementTypeDescription' where playerId = $playerId and round = $round";
	mysql_query($sql);
}

function setDefaultTeam($teamId, $round) {
	$sql = "INSERT INTO playerforround (playerId, playerPosition, emergency, round) select playerId, playerPosition, emergency, $round from playerforround where round = " . ($round - 1);
	$sql .= " and playerId in (select id from teamplayer where teamId = $teamId and traded = false);";
	mysql_query($sql);
	
	updateTeamSubmittedStatus($teamId);
}
?>